App.controller('registerController', ['$rootScope', '$scope', '$state', '$window', 'Page','settings','$interval','$timeout',
                                      function ($rootScope, $scope, $state, $window, Page,settings,$interval,$timeout) {
		$scope.register = {
			captchaUrl : settings.imgCaptchaUrl + '?appId=' + settings.appId + '&_d=' + Math.random(),
			agree:false,
			msg : {}
		};
		//更新图形验码
		$scope.updateCaptcha = function(){
			$scope.register.captchaUrl = settings.imgCaptchaUrl + '?appId=' + settings.appId + '&_d=' + Math.random();
		};
		
		$("#registerRealName").focus(function(){
			$scope.register.msg.realNameMsg = "";
	    });
		
		//验证用户信息
		$scope.valdateUserInfo = function(realName,nikeName,loginName){
			if(!realName || realName == ''){
				$scope.register.msg.realNameMsg = '用户姓名不能为空';
				return false;
			}
			if(!nikeName || nikeName == ''){
				$scope.register.msg.nikeNameMsg = '昵称不能为空';
				return false;
			}
			if(!loginName || loginName == ''){
				$scope.register.msg.loginNameMsg = '登录名称不能为空';
				return false;
			}
			return true;
		};
		//验证手机号码
		$scope.valdatePhone = function(phone){
			var reg = /^1[3|4|5|7|8]\d{9}$/;
			if(!reg.test(phone)){
				$scope.register.msg.phoneMsg = '请输入有效的手机号码';
				return false;
			}
			return true;
		};
		//验证密码
		$scope.valdatePassword = function(password,repassword){
			if(!password || password.length < 6){
				$scope.register.msg.passwordMsg = '密码长度不小于6位';
				return false;
			}
			if(!repassword || repassword.length < 6){
				$scope.register.msg.repasswordMsg = '密码长度不小于6位';
				return false;
			}
			if(password != repassword){
				$scope.register.msg.repasswordMsg = '密码不正确';
				return false;
			}
			return true;
		};
		//验证图形验证码
		$scope.valdateImageCaptcha = function(captcha){
			var reg = /^[A-Za-z0-9]{4}$/;
			if(reg.test(captcha)){
				return true;
			}else{
				$scope.register.msg.imageCaptchaMsg = "请输入有效图形验证码！";
				return false;
			}
		};
		
		//验证短信验证码
		$scope.valdateSmsCaptcha = function(captcha){
			var reg = /^[A-Za-z0-9]{6}$/;
			if(reg.test(captcha)){
				return true;
			}else{
				$scope.register.msg.smsCaptchaMsg = "请输入短信验证码！";
				return false;
			}
		};
		
		$scope.sendVertifiCode = function(){
			if($scope.valdateUserInfo($scope.register.realName,$scope.register.nikeName,$scope.register.loginName)
					&& $scope.valdatePhone($scope.register.phone) 
					&& $scope.valdatePassword($scope.register.password,$scope.register.repassword)
					&& $scope.valdateImageCaptcha($scope.register.imageCaptcha)){
				var param = {
					appId: settings.appId,
					phone: $scope.register.phone,
					captcha : $scope.register.imageCaptcha
				};
				Page.ajaxPostQuiet(settings.smsCaptchaUrl, param, function(resp) {
					Page.notice('验证码已发送');
	         	  	$scope.register.vertifiCodeTime = 60;
					$scope.register.vertifiCodeTimer = $interval(function(){
		                $scope.register.vertifiCodeTime -- ;
		                if($scope.register.vertifiCodeTime <= 0){
		                	$interval.cancel($scope.register.vertifiCodeTimer);
		                }
		            },1000,60);
	        	},function(resp){
	        		$scope.updateCaptcha();
	        	});
			}else{
				$scope.updateCaptcha();
			}
		};
		
		$scope.submitRegister = function () {
			if($scope.valdateUserInfo($scope.register.realName,$scope.register.nikeName,$scope.register.loginName)
					&& $scope.valdatePhone($scope.register.phone) 
					&& $scope.valdatePassword($scope.register.password,$scope.register.repassword)
					&& $scope.valdateSmsCaptcha($scope.register.smsCaptcha)){
				$("#registerBtn").attr("disabled",true);
    			$timeout(function(){
    				$("#registerBtn").attr("disabled",false);
	            },5000);
    			
				var param = {
				    phone: $scope.register.phone,
				    captcha: $scope.register.smsCaptcha, // 短信验证码
				    loginName: $scope.register.loginName,
				    realName: $scope.register.realName,
				    nikeName: $scope.register.nikeName,
				    password: $.md5($scope.register.password) // md5加密
				};
				Page.ajaxPostQuiet(settings.registerUrl, param, function(resp) {
					$rootScope.user = resp.data;
					Page.notice('注册成功!');
					$state.go('login');
				},function(resp){
					$scope.register.msg.errorMsg = resp.resultDes;
					$scope.updateCaptcha();
				});
			}
	    };
	
	
}]);